package krt.wid.gslzs.util;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import krt.wid.gslzs.R;


/**
 * @author Marcus
 * @package com.krt.xmt.manager
 * @description
 * @time 2017/7/25
 */

public class TitleManager {
    private Context mContext;
    //左边为图片或者文字
    private boolean isLeftText;

    private boolean isRightText;
    private FrameLayout mLeftView;
    private FrameLayout mRightView;
    private FrameLayout mCenterView;
    private RelativeLayout mTitleLayout;
    private LeftClickListener mLeftClickListener;
    private View divider;
    private ImageView leftImg;

    private RightClickListener mRightClickListener;


    public interface LeftClickListener {
        void leftClick();
    }

    public interface RightClickListener {
        void rightClick();
    }

    public TitleManager(Activity context) {
        mContext = context;
        mLeftView = (FrameLayout) context.findViewById(R.id.left_layout);
        mRightView = (FrameLayout) context.findViewById(R.id.right_layout);
        mCenterView = (FrameLayout) context.findViewById(R.id.center_layout);
        mTitleLayout = (RelativeLayout) context.findViewById(R.id.title_layout);
        divider = context.findViewById(R.id.divider);
        mTitleLayout.setBackgroundColor(ContextCompat.getColor(mContext,R.color.color_1b9ef6));
        setLeftImage(0);
    }

    public TitleManager(View view, Activity context) {
        mContext = context;
        mLeftView = (FrameLayout) view.findViewById(R.id.left_layout);
        mRightView = (FrameLayout) view.findViewById(R.id.right_layout);
        mCenterView = (FrameLayout) view.findViewById(R.id.center_layout);
        mTitleLayout = (RelativeLayout) context.findViewById(R.id.title_layout);
        mTitleLayout.setBackgroundColor(0xfffffff);
        divider = context.findViewById(R.id.divider);
        setLeftImage(0);
    }

    public RelativeLayout getTitleLayout() {
        return mTitleLayout;
    }

    public void setTitleLayout(RelativeLayout titleLayout) {
        mTitleLayout = titleLayout;
    }

    public FrameLayout getCenterView() {
        return mCenterView;
    }

    public FrameLayout getRightView() {
        return mRightView;
    }



    /**
     * 当右边为文字的时候
     */
    public void setRightText(String text) {
        setRightText(text, 0);
    }

    public void setRightText(String text, int textColor, int textSize) {
        TextView textView = new TextView(mContext);
        textView.setText(text);
        if (textColor != 0) {
            textView.setTextColor(textColor);
        }
        if (textSize != 0) {
            textView.setTextSize(textSize);
        }
        mRightView.addView(textView);
    }

    public void setLeftVisibility (){
        mLeftView.setVisibility(View.INVISIBLE);
    }

    /**
     * 当中间问文字的时候设置文字颜色
     */
    public void setRightText(String text, int textColor) {
        TextView textView = new TextView(mContext);
        textView.setText(text);
        textView.setTextSize(14);
        if (textColor != 0) {
            textView.setTextColor(textColor);
        }
        mRightView.addView(textView);
    }

    /**
     * 当中间为文字的时候
     */
    public void setCenterText(String text) {
        setCenterText(text, 0);
    }

    /**
     * 当中间问文字的时候设置文字颜色
     *
     * @param textSize  单位为像素
     * @param textColor 如果只设置为#FFFFFF会有色差,格式为0x(标记)FF(不透明)+FFFFFF
     */
    public void setCenterText(String text, int textColor, int textSize) {
        TextView textView = new TextView(mContext);
        textView.setText(text);
        textView.setTextSize(16);
        textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666));
        if (textSize != 0) {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
        }
        if (textColor != 0) {
            textView.setTextColor(textColor);
        }
        mCenterView.addView(textView);
    }

    /**
     * textsize 单位为像素
     *
     * @param text
     * @param textColor
     */
    public void setCenterText(String text, int textColor) {
        setCenterText(text, textColor, 0);
    }

    /**
     * 当左边为图片时
     *
     * @param res 图片资源
     */
    public void setLeftImage(int res) {
        if (res == 0) {
            res = R.mipmap.sy_return;
        }
        if (leftImg == null) {
            leftImg = new ImageView(mContext);
            leftImg.setImageResource(res);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(38, 38);
            leftImg.setLayoutParams(lp);
            mLeftView.addView(leftImg);
        }else{
            leftImg.setImageResource(res);
        }


    }

    public void visableLeftImage(boolean visable) {
        mLeftView.setVisibility(visable ? View.VISIBLE : View.GONE);
    }

    /**
     * 当右边为图片时
     *
     * @param
     */
    public void setRightImage(int res) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(res);
        mRightView.addView(imageView);
    }

    /**
     * 隐藏分割线
     */
    public void hideDivider() {
        divider.setVisibility(View.GONE);
    }

    /**
     * 当中间为一个视图的时候
     */
    public void setCenterView(View view) {
        mCenterView.addView(view);
    }

    /**
     * 设置title的背景颜色
     *
     * @param color 如果只设置为#FFFFFF会有色差,格式为0x(标记)FF(不透明)+FFFFFF
     */
    public void setBackgroudColor(int color) {
        mTitleLayout.setBackgroundColor(color);
    }


}
